<?php

/**
 * alltosun.com admin_data 统计数据
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2016-4-6 下午5:58:21 $
 * $Id: $
 */
class admin_data_helper
{
    private $enable_user_ids = array();
    private $enable_store_ids = array();

    /**
     * 获取设备绑定信息
     */
    public static function get_device_record_list($time = "") {
        if (!$time) {
            $time = strtotime('-1 day');
        }

        $start_time = date('Y-m-d', $time) . ' 00:00:00';
        $end_time = date('Y-m-d', $time) . ' 23:59:59';

        // 获取所有已经绑定的保温箱码和广告机码
        $machine_no_list = device_helper::get_device_no_list_by_type('MX');
        $adbox_no_list = device_helper::get_device_no_list_by_type('AD');

        $long_list = array();
        $short_list = array();
        $long_type = '';
        $short_type = '';

        if (count($machine_no_list) >= count($adbox_no_list)) {
            $long_list = $machine_no_list;
            $short_list = $adbox_no_list;
            $long_type = 'machine';
            $short_type = 'adbox';
        } else {
            $long_list = $adbox_no_list;
            $short_list = $machine_no_list;
            $long_type = 'adbox';
            $short_type = 'machine';
        }
        $device_list = array();

        if ($long_list) {
            foreach ($long_list as $k => $v) {
                $long_store_device_relation = device_helper::get_store_device_relation($v['id']);
                if (!$long_store_device_relation) {
                    // 如果找不到设备绑定信息跳出
                    //unset($long_list[$k]);
                    continue;
                }

                if ($long_store_device_relation['add_time'] >= $end_time ||
                    $long_store_device_relation['add_time'] <= $start_time
                ) {
                    //unset($long_list[$k]);
                    continue;
                }
                $long_store_info = store_helper::get_store($long_store_device_relation['store_id']);
                if (!$long_store_info) {
                    // 如果找不到门店信息
                    // unset($long_list[$k]);
                    continue;
                }
                $long_dealer_info = user_helper::get_user_info($long_store_info['agency_id']);

                if (1 == $long_store_info['is_enable'] &&
                    1 == $long_store_info['is_delete'] &&
                    1 == $long_dealer_info['is_enable'] &&
                    1 == $long_dealer_info['status']
                ) {
                    if (!$long_store_device_relation || !$long_store_info || !$long_dealer_info) {
                        // 如果找不到商店信息可能是商店被删除了
                        //unset($long_list[$k]);
                        continue;
                    }

                    if (empty($device_list[$k])) {
                        $device_list[$k] = array();
                    }

                    $device_list[$k][$long_type . '_add_time'] = substr($long_store_device_relation['add_time'], 0, 10);
                    $device_list[$k][$long_type . '_device_no'] = $v[$long_type . '_no'];
                    $device_list[$k][$long_type . '_status'] = '已绑定';
                    $device_list[$k][$long_type . '_store_name'] = $long_store_info['title'] .
                                                                   $long_store_info['title_branch'];
                    $device_list[$k][$long_type . '_dealer_id'] = $long_dealer_info['id'];
                    $device_list[$k][$long_type . '_dealer_name'] = $long_dealer_info['realname'];
                    $device_list[$k][$long_type . '_bind_time'] = $long_store_device_relation['add_time'];
                    $device_list[$k][$short_type . '_add_time'] = '';
                    $device_list[$k][$short_type . '_device_no'] = '';
                    $device_list[$k][$short_type . '_status'] = '';
                    $device_list[$k][$short_type . '_store_name'] = '';
                    $device_list[$k][$short_type . '_dealer_name'] = '';
                    $device_list[$k][$short_type . '_bind_time'] = '';
                }
            }
        }

        if ($short_list) {
            foreach ($short_list as $k => $v) {
                $short_store_device_relation = device_helper::get_store_device_relation($short_list[$k]['id']);

                if (empty($short_store_device_relation)) {
                    continue;
                }
                $short_store_info = store_helper::get_store($short_store_device_relation['store_id']);
                $short_dealer_info = user_helper::get_user_info($short_store_info['agency_id']);

                if (empty($short_store_device_relation) || empty($short_store_info) || empty($short_dealer_info)) {
                    continue;
                }

                if (1 == $short_store_info['is_enable'] &&
                    1 == $short_store_info['is_delete'] &&
                    1 == $short_dealer_info['is_enable'] &&
                    1 == $short_dealer_info['status']
                ) {

                    if ($short_store_device_relation['add_time'] >= $end_time ||
                        $short_store_device_relation['add_time'] <= $start_time
                    ) {
                        //unset($short_list[$k]);
                        continue;
                    }

                    if (empty($device_list[$k])) {
                        $device_list[$k] = array();
                    }

                    $device_list[$k][$short_type . '_add_time'] = $short_store_device_relation['add_time'];
                    $device_list[$k][$short_type . '_device_no'] = $short_list[$k][$short_type . '_no'];
                    $device_list[$k][$short_type . '_status'] = '已绑定';
                    $device_list[$k][$short_type . '_store_name'] = $short_store_info['title'] .
                                                                    $short_store_info['title_branch'];
                    $device_list[$k][$short_type . '_dealer_name'] = $short_dealer_info['realname'];
                    $device_list[$k][$short_type . '_bind_time'] = $short_store_device_relation['add_time'];
                }
            }
        }

        return $device_list;
    }


    /**
     * 获取商家明细
     */
    public static function get_store_data_list($time = "") {
        if (!$time) {
            $time = strtotime('-1 day');
        }
        $start_time = date('Y-m-d', $time) . ' 00:00:00';
        $end_time = date('Y-m-d', $time) . ' 23:59:59';
        $store_list = array();

        $filter['is_enable'] = 1;
        $filter['is_delete'] = 1;
        $filter['`agency_id` != '] = 0;
        $filter['`add_time` >= '] = $start_time;//date('Y-m-d').' 00:00:00';
        $filter['`add_time` <= '] = $end_time;

        $new_store_ids = _model('store')->getFields('id', $filter, ' ORDER BY `id` DESC');
        $dealer_order_store_ids = order_helper::get_store_ids_by_update_time(array(3, 5, 10), 1, array(
            'is_enable' => 1,
            'status' => 1,
            '`update_time` >= ' => $start_time,
            '`update_time` <= ' => $end_time
        ));
        foreach ($dealer_order_store_ids as $k => $v) {
            if ($dealer_order_store_ids[$k] == 0) {
                unset($dealer_order_store_ids[$k]);
                continue;
            }
        }

        $store_ids = array_unique(array_merge($new_store_ids, $dealer_order_store_ids));

        foreach ($store_ids as $k => $v) {
            $store_list[] = store_helper::get_store($v);
        }
        if ($store_list) {
            $type_arr = store_config::$type_arr;
            foreach ($store_list as $k => $v) {
                if (!$v['sign_user_id']) {
                    continue;
                }
                $sign_user_role_id = user_helper::get_user_role_id($v['sign_user_id']);
                $sign_user_info = user_helper::get_user_info($v['sign_user_id']);
                $dealer_user_info = user_helper::get_user_info($v['agency_id']);
                if (!$sign_user_info) {
                    // unset($store_list[$k]);
                    // 如果找不到签约人,则默认使用经销商的信息
                    $sign_user_info = $dealer_user_info;
                }

                if (!$dealer_user_info) {
                    // unset($store_list[$k]);
                    continue;
                }

                $user_info = _uri('user', @$v['owner_id']);

                if ($user_info) {
                    $store_list[$k]['user_name'] = $user_info['user_name'];
                    $store_list[$k]['mobile'] = $user_info['mobile'];
                }
                if (isset($type_arr[$v['type']])) {
                    $store_list[$k]['type_name'] = $type_arr[$v['type']];
                }
                if ($v['agency_id']) {
                    $dealer_info = _uri('user', $v['agency_id']);
                    if ($dealer_info) {
                        $store_list[$k]['dealer_name'] = $dealer_info['realname'];
                        $store_list[$k]['dealer_mobile'] = $dealer_info['mobile'];
                    }
                }

                $store_list[$k]['sign_user_type'] = user_config::$role_arr[$sign_user_role_id];
                $store_list[$k]['sign_user_role_id'] = user_config::$role_arr[$sign_user_role_id];
                $store_list[$k]['sign_user_info'] = $sign_user_info;
                $store_list[$k]['sign_user'] = $sign_user_info['realname'] .
                                               '（' .
                                               $store_list[$k]['sign_user_type'] .
                                               '）';

                // 是否进货及进货额度
                $store_list[$k]['is_ever_stock'] = store_helper::get_store_stock_money($v['id'], 10);
                $store_list[$k]['stock_num'] = 0;
                if ($store_list[$k]['is_ever_stock']) {
                    $store_list[$k]['stock_num'] = $store_list[$k]['is_ever_stock'] . '箱';
                    $store_list[$k]['is_ever_stock'] = '是';
                } else {
                    $store_list[$k]['is_ever_stock'] = '否';
                    $store_list[$k]['stock_num'] = '0箱';
                }
                $store_list[$k]['store_title'] = $store_list[$k]['title'] . $store_list[$k]['title_branch'];
                $store_list[$k]['store_stock_money'] = store_helper::get_store_stock_money($v['id']);
                $store_list[$k]['store_type'] = store_config::$type_arr[$v['type']];
                if ($v['status'] == 1) {
                    $store_list[$k]['store_status'] = '未审核';
                } elseif ($v['status'] == 2) {
                    $store_list[$k]['store_status'] = '正常营业';
                } else {
                    $store_list[$k]['store_status'] = '已撤店';
                }

                // 所属合伙人
                $store_list[$k]['dealer_name'] = $dealer_user_info['realname'];

                // 是否绑定设备
                $is_bind_device = device_helper::is_bind_device($v['id']);
                if ($is_bind_device) {
                    $store_list[$k]['is_bind_device'] = '是';
                } else {
                    $store_list[$k]['is_bind_device'] = '否';
                }

            }
        }

        return $store_list;
    }


    /**
     * 获取合伙人明细
     */
    public static function get_dealer_info_list() {
        $enable_user_ids = _model('user')->getFields('id', array(
            'is_enable' => 1,
            'status' => 1,
            '`add_time` <= ' => date('Y-m-d H:i:s') . ' 23:59:59'
        ));
        //         $this->enable_store_ids = _model('store')->getFields('id', array('is_enable' => 1, 'is_delete' => 1, '`add_time` <= '=>date('Y-m-d H:i:s').' 23:59:59'));


        // 获取所有有效的经销商
        $dealer_ids = user_helper::get_role_user_ids(user_config::AGENCY, '', array('is_enable' => 1, 'status' => 1));
        if (!$dealer_ids) {
            return array();
        }

        $dealer_list = array();
        foreach ($dealer_ids as $k => $v) {
            $cur_dealer_info = user_helper::get_user_info($v);
            //             if (!$cur_dealer_info) {
            //                 // unset($dealer_ids[$k]);
            //                 continue;
            //             }
            // 日期
            $dealer_list[$k] = array();
            $dealer_list[$k]['dealer_id'] = $cur_dealer_info['id'];
            $dealer_list[$k]['add_time'] = $cur_dealer_info['add_time'];
            // 新增合伙人姓名
            $dealer_list[$k]['dealer_name'] = $cur_dealer_info['realname'] ? $cur_dealer_info['realname']
                : $cur_dealer_info['nickname'];
            // 电话
            $dealer_list[$k]['dealer_mobile'] = ' ' . $cur_dealer_info['mobile'] . ' ';
            // 是否创建库存 @TODO
            $dealer_list[$k]['is_create_stock'] = ' ';
            // 掌柜数量
            $store_list = store_helper::get_store_list_by_dealer_id($cur_dealer_info['id'], array(
                'is_enable' => 1,
                'is_delete' => 1,
                'status' => array(1, 2)
            ));
            $dealer_list[$k]['store_num'] = count($store_list);
            // 是否创建掌柜
            if ($store_list) {
                $dealer_list[$k]['is_sign_store'] = '是';

                $bind_device_num = 0;
                $marketing_list = array();
                $marketing_num = 0;
                $is_sign_marketing = '否';
                foreach ($store_list as $key => $val) {
                    if (0 != $val['marketing_id'] &&
                        $val['marketing_id'] != $val['agency_id'] &&
                        !in_array($val['marketing_id'], $marketing_list)
                    ) {
                        array_push($marketing_list, $val['marketing_id']);
                    }
                    $marketing_num = count($marketing_list);
                    if ($marketing_num) {
                        $is_sign_marketing = '是';
                    } else {
                        $is_sign_marketing = '否';
                    }
                    $cur_device_num = 0;
                    // 当天店铺的绑定设备数
                    $cur_device_num = device_helper::get_bind_device_type_count_by_store_id($val['id']);
                    $bind_device_num += $cur_device_num;
                }

                // 员工数量
                $dealer_list[$k]['marketing_num'] = _model('user_marketing')->getTotal(array(
                    'agency_id' => $v,
                    'marketing_id' => $enable_user_ids
                ));
                if ($dealer_list[$k]['marketing_num']) {
                    $is_sign_marketing = '是';
                } else {
                    $is_sign_marketing = '否';
                }
                // 是否创建员工
                $dealer_list[$k]['is_sign_marketing'] = $is_sign_marketing;

                // 绑定设备数量
                $dealer_list[$k]['bind_device_num'] = $bind_device_num;
            } else {
                $dealer_list[$k]['is_sign_store'] = '否';
                // 是否创建员工
                $dealer_list[$k]['is_sign_marketing'] = '否';
                // 员工数量
                $dealer_list[$k]['marketing_num'] = '0';
                // 绑定设备数量
                $dealer_list[$k]['bind_device_num'] = '0';
            }

            // 下单数量 (是要是收款的订单就算)
            $dealer_list[$k]['dealer_order_num'] = order_helper::get_order_num_by_progress(array(
                3,
                5,
                10
            ), 1, array('user_id' => $cur_dealer_info['id'], 'is_enable' => 1, 'status' => 1));
            // 是否下单
            if ($dealer_list[$k]['dealer_order_num']) {
                $dealer_list[$k]['is_ever_order_goods'] = '是';
            } else {
                $dealer_list[$k]['is_ever_order_goods'] = '否';
            }

            // 待收款
            $dealer_list[$k]['prepare_pay_order_num'] = order_helper::get_order_num_by_progress(1, 1, array(
                'user_id' => $cur_dealer_info['id'],
                'is_enable' => 1,
                'status' => 1
            ));;
            // 待配送
            $dealer_list[$k]['prepare_delivery_order_num'] = order_helper::get_order_num_by_progress(3, 1, array(
                'user_id' => $cur_dealer_info['id'],
                'is_enable' => 1,
                'status' => 1
            ));;
            // 配送中
            $dealer_list[$k]['in_delivery_order_num'] = order_helper::get_order_num_by_progress(5, 1, array(
                'user_id' => $cur_dealer_info['id'],
                'is_enable' => 1,
                'status' => 1
            ));;
            // 配送完成
            $dealer_list[$k]['finish_delivery_order_num'] = order_helper::get_order_num_by_progress(10, 1, array(
                'user_id' => $cur_dealer_info['id'],
                'is_enable' => 1,
                'status' => 1
            ));;
            // 撤销订单
            $dealer_list[$k]['dealer_cancel_order_num'] = order_helper::get_order_num_by_progress(15, 1, array(
                'user_id' => $cur_dealer_info['id'],
                'is_enable' => 1,
                'status' => 1
            ));;
        }

        return $dealer_list;
    }


    /**
     * 获取指定一天的统计数据
     * @param unknown $time 时间戳
     * @return number
     */
    public static function get_admin_data($time = '') {
        if (!$time) {
            $time = strtotime('-1 day');
        }

        $cur_day_filter = array(
            '`add_time` > ' => date('Y-m-d', $time) . ' 00:00:00',
            '`add_time` <= ' => date('Y-m-d', $time) . ' 23:59:59'
        );
        $pre_day_filter = array('`add_time` <= ' => date('Y-m-d', $time) . ' 23:59:59');

        $admin_data_list = array();

        // 经销商相关数据
        // 经销商数量
        $admin_data_list['dealer_num'] = user_helper::get_user_num_by_role_id(user_config::AGENCY, $pre_day_filter);

        // 今日新增：多少人增长率多少
        $admin_data_list['today_add_dealer_num'] = user_helper::get_user_num_by_role_id(user_config::AGENCY, $cur_day_filter);
        $before_dealer_num = user_helper::get_user_num_by_role_id(user_config::AGENCY, array(
            '`add_time` < ' => date('Y-m-d', $time) .
                               ' 00:00:00'
        ));

        // new 今日新增加的订单
        $admin_data_list['today_dealer_new_order_num'] = order_helper::get_order_num_by_progress(array(
            1,
            3,
            5,
            10
        ), 1, $cur_day_filter);

        // new 今日订单总数(包括已经撤销的订单)
        $admin_data_list['today_dealer_order_num'] = order_helper::get_order_num_by_progress(array(
            1,
            3,
            5,
            10,
            15
        ), 1, $cur_day_filter);

        // new 今日撤销订单数
        $admin_data_list['today_dealer_cancel_order_num'] = order_helper::get_order_num_by_progress(15, 1, $cur_day_filter);

        // new 已成功出货的订单数
        // 已成功出货：多少箱 (总部向合伙人发货)
        $admin_data_list['delivery_goods_num'] = order_helper::get_order_goods_num(array(5, 10), 1, $pre_day_filter);

        // 待收款订单/等待收款项：订单数量/（未成交金额）多少元  @FIXME

        // 等待发货：多少箱 (只要提交订单就算是) @FIXME(名字不对 原来是prepare_delivery_order_num)
        $admin_data_list['prepare_delivery_goods_num'] = order_helper::get_order_goods_num(array(
            1,
            3
        ), 1, $pre_day_filter);

        // new 等待发货的订单数
        $admin_data_list['prepare_delivery_order_num'] = order_helper::get_order_num_by_progress(array(
            1,
            3
        ), 1, $pre_day_filter);
        // @TODO (未)创建库存人数
        $admin_data_list['is_create_stock_person_num'] = '无';
        $admin_data_list['not_create_stock_person_num'] = '无';

        // 合伙人总库存：多少箱
        $dealer_ids = user_helper::get_role_user_ids(user_config::AGENCY, '', array(
            'is_enable' => 1,
            '`add_time` <= ' => date('Y-m-d', $time) . ' 23:59:59'
        ));
        $admin_data_list['all_dealer_stock'] = dealers_helper::get_dealer_stock($dealer_ids);

        // 合伙人总员工：多少人 (市场人员的总数量)
        $store_list = store_helper::get_store_list_by_dealer_id($dealer_ids, array(
            'is_enable' => 1,
            'is_delete' => 1,
            '`add_time` <= ' => date('Y-m-d', $time) . ' 23:59:59'
        ));
        $marketing_ids = array();
        if ($store_list) {
            foreach ($store_list as $k => $v) {
                if (!in_array($v['marketing_id'], $marketing_ids) &&
                    $v['marketing_id'] != $v['agency_id'] &&
                    $v['marketing_id'] != 0
                ) {
                    array_push($marketing_ids, $v['marketing_id']);
                }
            }
        }

        $enable_user_ids = _model('user')->getFields('id', $pre_day_filter);
        $admin_data_list['marketing_num'] = _model('user_marketing')->getTotal(array(
            'agency_id' => $dealer_ids,
            '`marketing_id`' => $enable_user_ids,
            '`add_time` <= ' => date('Y-m-d H:i:s') . ' 23:59:59'
        ));

        // 终端店铺相关数据(分店)
        $admin_data_list['store_num'] = store_helper::get_all_store_num(array(
            '`add_time` <= ' => date('Y-m-d', $time) .
                                ' 23:59:59',
            'is_enable' => 1,
            'is_delete' => 1
        ));
        // 今日新增：多少门店增长率多少
        $today_add_store_num = user_helper::get_user_num_by_role_id(user_config::STORE_MANAGER, $cur_day_filter);
        $admin_data_list['today_add_store_num'] = $today_add_store_num;
        $before_store_num = user_helper::get_user_num_by_role_id(user_config::STORE_MANAGER, $pre_day_filter);

        // 已进货门店数量：(必须是确认收货的门店)
        $admin_data_list['is_stock_store_num'] = user_helper::get_user_num_by_role_id(user_config::STORE_MANAGER, array(
            '`score` > ' => 0,
            '`add_time` <= ' => date('Y-m-d', $time) . ' 23:59:59'
        ));
        // 未进货门店数量：(其他)
        $admin_data_list['not_stock_store_num'] = $admin_data_list['store_num'] -
                                                  $admin_data_list['is_stock_store_num'];
        // 今日新增订单量：(统计处在配送中的订单,不会撤销的订单)
        $admin_data_list['store_new_order_num'] = order_helper::get_order_num_by_progress(array(
            1,
            3,
            5,
            10
        ), 5, $cur_day_filter);
        $admin_data_list['add_time'] = date('Y-m-d H:i:s');

        // new 当天撤销的订单数
        $admin_data_list['today_store_cancel_order_num'] = order_helper::get_order_num_by_progress(15, 5, $cur_day_filter);
        // new 当天的订单总数
        $admin_data_list['today_store_order_num'] = order_helper::get_order_num_by_progress(array(
            1,
            3,
            5,
            10,
            15
        ), 5, $cur_day_filter);

        // new 合伙人签约的门店数量
        $dealer_ids = user_helper::get_role_user_ids(user_config::AGENCY, '', $pre_day_filter);
        $dealer_ids = _model('user')->getFields('id', array('is_enable' => 1, 'status' => 1, '`dealer_level` > ' => 0));
        $admin_data_list['dealer_sign_store_num'] = store_helper::get_all_store_num(array(
            '`add_time` <= ' => date('Y-m-d', $time) .
                                ' 23:59:59',
            'sign_user_id' => $dealer_ids
        ));
        // new 市场人员签约的门店数量
        $marketing_ids = user_helper::get_role_user_ids(user_config::MARKETING, '', $pre_day_filter);
        $admin_data_list['marketing_sign_store_num'] = store_helper::get_all_store_num(array(
            '`add_time` <= ' => date('Y-m-d', $time) .
                                ' 23:59:59',
            '`sign_user_id` != ' => 0,
            '`marketing_id` NOT ' => $dealer_ids
        ));
        // new 待审核门店数
        $admin_data_list['trial_store_num'] = store_helper::get_all_store_num(array(
            '`add_time` <= ' => date('Y-m-d', $time) .
                                ' 23:59:59',
            'status' => 1
        ));
        // new 正常营业的门店数
        $admin_data_list['normal_store_num'] = store_helper::get_all_store_num(array(
            '`add_time` <= ' => date('Y-m-d', $time) .
                                ' 23:59:59',
            'status' => 2
        ));
        // new 已撤店门店数
        $admin_data_list['stop_store_num'] = store_helper::get_all_store_num(array(
            '`add_time` <= ' => date('Y-m-d', $time) .
                                ' 23:59:59',
            'status' => 3
        ));

        return $admin_data_list;
    }


}


?>